using System;
using System.Linq;
using Giraffe.Client.Common;
using Giraffe.Client.Common.Managers;
using Giraffe.Core.Common.Messengers;
using Giraffe.Core.Model;
using Microsoft.Practices.Composite.Presentation.Commands;
using Giraffe.Core.Common;

namespace Giraffe.Client.MainWindowModule.Contacts
{
    public class ContactViewModel: ViewModelBase
    {
        private IContact _contact;
        private DelegateCommand<object> _writeToCommand;

        public ContactViewModel( IContact contact)
        {
            _contact = contact;
            _contact.StateChanged += UpdateContactInfo;
        }

        public string Name
        {
            get
            {
                return _contact.Name;
            }
        }

        public bool IsOnline
        {
            get
            {
                return _contact.State == ContactStateEnum.Online;
            }
        }

        private void UpdateContactInfo()
        {
            RisePropertyChanged("IsOnline");
        }

        public DelegateCommand<object> WriteToCommand
        {
            get
            {
                if (_writeToCommand == null)
                {
                    _writeToCommand = new DelegateCommand<object>(WriteToCommandExecute);
                }
                return _writeToCommand;
            }
        }

        private void WriteToCommandExecute(object obj)
        {
            var conversationManager = ObjectProvider.Resolve<IConversationManagerPresentation>();
            //todo needs to change logic, needs messenger coversations priority or durectly link
            conversationManager.StartNewConversation(_contact, _contact.MessengerContacts.First().Messenger);
        }
    }
}